import { showToast } from '../../utils/util'
var app = getApp()

Page({
	data: {
		sellers_id: 0
	},
	onLoad: function (query) {
		app.getUrl(this)
		app.getSystem(this)
		this.setData({
			store_info: wx.getStorageSync('store_info'),
			type: query.type
		})
	},
	onShow: function () {
		this.refresh(() => {
			this.getBusinessInfo()
		})
	},
	/**
	 * @function 下拉刷新
	 */
	onPullDownRefresh() {
		this.getBusinessInfo(() => {
			wx.stopPullDownRefresh()
		})
	},
	/**
	 * @function 获取商家信息
	 * @param {*} cb 请求成功的回调cb，因为之前封装没有promise
	 */
	getBusinessInfo(cb) {
		app.util.request({
			url: 'seller/getAppSeller',
			cachetime: '0',
			data: {
				seller_id: this.data.sellers_id
			},
			testapi: app.siteInfo.siteroot_admin,
			success: res => {
				this.setData({
					store_info: res.data.data
				})
				cb && cb()
			}
		})
	},
	refresh: function (cb) {
		var a = this
		if (2 == a.data.type) {
			var e = wx.getStorageSync('userInfo').id
			app.util.request({
				url: 'entry/wxapp/StoreWxLogin',
				cachetime: '0',
				data: {
					user_id: e
				},
				success: function (t) {
					console.log(t.data.seller_id, 't.data.seller_id')
					a.setData(
						{
							store_info: t.data,
							sellers_id: t.data.seller_id
						},
						cb
					)
					app.util.request({
						url: 'entry/wxapp/TxMoney',
						cachetime: '0',
						data: {
							seller_id: t.data.seller_id
						},
						success: function (t) {
							a.setData({
								price: t.data
							})
						}
					})
				}
			})
		} else {
			var s = wx.getStorageSync('sign')
			wx.getStorageSync('store_info')
			app.util.request({
				url: 'entry/wxapp/HtLogin',
				cachetime: '0',
				data: {
					username: s.name,
					password: s.password
				},
				success: res => {
					a.setData({
						sellers_id: res.data.seller_id
					})
					this.getBusinessInfo()
				}
			})
		}
	},
	/**
	 * @function 跳转到商家订单列表
	 */
	goBusinessOrderList(e) {
		wx.navigateTo({
			url: `../businessOrderList/index?index=${e.currentTarget.dataset.index}`
		})
	},
	/**
	 * @function 跳转到商家订单列表
	 */
	goBusinessHouseManage(e) {
		wx.navigateTo({
			url: `../businessHouseManage/index`
		})
	},
	/**
	 * @function 核销核销码
	 */
	code: function () {
		wx.scanCode({
			success: res => {
				console.log('扫到的code：', res)
				app.util.request({
					url: 'order/confirmCheckin',
					cachetime: '0',
					data: {
						confirm_type: '1',
						id: res.result
					},
					testapi: app.siteInfo.siteroot_admin,
					success: res => {
						// 0:成功，-1：失败，-2：登录失效，-3：权限不足
						// 这边加上延迟，如果不加，toast一下子就消失了；
						if (0 == res.data.code) {
							setTimeout(() => {
								showToast('订单核销成功', 2000)
							}, 500)
						} else {
							setTimeout(() => {
								showToast('核销失败', 2000)
							}, 500)
						}
					}
				})
			}
		})
	}
})
